package com.windy.aop;

import com.windy.exection.TokenException;
import com.windy.service.TokenService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Component
@Aspect
public class TokenAspect {

    @Resource
    TokenService tokenService;

    @Pointcut("@annotation(com.windy.annotation.TokenAnnotation)")
    public void pointCut(){}

    @Before(("pointCut()"))
    public void before(JoinPoint joinPoint) throws TokenException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        try {
            tokenService.checkToken(request);
        }catch (TokenException e){
            throw e;
        }
    }
}
